// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
import w from './world';
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    private cid=0;
    private world:w;
    public init(ny,sd){
        this.node.y=ny;
        this.speed=sd;
        this.world=this.node.addComponent('world');
    }
    onCollisionEnter(other,self){
        if(other.node.name=="aoteman_ske"){return;}
        if(other.tag==0&&self.tag==8){
            if(self.tid==null){self.tid=0;}
            self.tid++;
        }else if(other.tag==0&&self.tag==9){
            this.node.scaleX*=-1;
        }else if(other.tag==0&&self.tag==5){
            if(this.cid==null){this.cid=0;}
            this.cid++;
        }
    }
    onCollisionExit(other,self){
        if(other.node.name=="aoteman_ske"){return;}
        if(other.tag==0&&self.tag==8){
            self.tid--;
            if(self.tid<=0){
               this.node.scaleX*=-1;
            }
        }else if(other.tag==0&&self.tag==5){
            this.cid--;
        }
    }
    private speed=5;
    update (dt) {
         if(this.world.contains(this.node.x+50*this.node.scaleX*-1,this.node.y+this.node.height/2)||!this.world.contains(this.node.x+50*this.node.scaleX*-1,this.node.y)){
            this.node.scaleX*=-1;
         }
        if(this.node.scaleX>0){
            this.node.x-=this.speed;
        }else{
            this.node.x+=this.speed;
        }

     }
}
